﻿using System;
using System.Windows.Input;

namespace Playbook.Core
{
    /// <summary>
    /// Represents a command for binding to views.
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public class SimpleCommand<T> : ICommand<T>
    {
        public Predicate<T> CanExecuteDelegate { get; set; }
        public Action<T> ExecuteDelegate { get; set; }

        public bool CanExecute(T parameter)
        {
            if (CanExecuteDelegate != null) return CanExecuteDelegate(parameter); return true;
        }

        public event EventHandler CanExecuteChanged
        {
            add { CommandManager.RequerySuggested += value; }
            remove { CommandManager.RequerySuggested -= value; }
        }

        public void Execute(T parameter) { if (ExecuteDelegate != null)  ExecuteDelegate(parameter); }
    }
}
